package single1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 单例模式会被反射破坏
 *
 *
 * 通过反射也可以创建处对象，所以 单例模式也不是安全的
 */
public class Single6 {

    private Single6 getObject() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Class<?> c = Single6.class;

        Constructor result = c.getDeclaredConstructor(null);

        result.setAccessible(true);

        Single6 object = (Single6) result.newInstance();

        return object;
    }


    private Single6(){
        if(Single6.Inner.lazy != null){
            throw new RuntimeException("不容许创建多个实例");
        }
    }


    private static final Single6 getInstance(){
        return Single6.Inner.lazy;
    }

    private static class Inner{
        private static final Single6 lazy = new Single6();
    }
}
